<?php
if( !defined('IN_INDEX') )
{
	die('This page is hidden.');
}

if(!checkSession())
{
			function redirect($url)
			{
			?>
				<script type="text/javascript">
					{
					window.location = "<?=$url?>";
					}
				</script>
			<?php 
			}
			redirect("index.php?action=login");
	exit;
}
if($USER->getRank() >= 5)
{
?>
<?php
include('inc/functions_blog.php');
$time_now = time();
$news['title'] = "";
$news['comment'] = "";
$news['category'] = "";
$news['day'] = date('d', $time_now);
$news['month'] = date('m', $time_now);
$news['year'] = date('Y', $time_now);
$disable_comments = 0;

$missing = array();

if(isset($_POST['news_submit']))
{
	$news['title']=$_POST['news_title'];
	$news['category'] =$_POST['news_category'];
	$news['comment']=str_replace("'", "", $_POST['news_message']);
	$news['day']=$_POST['day'];
	$news['month']=$_POST['month'];
	$news['year']=$_POST['year'];
	if(isset($_POST['news_disablecomments']) && $_POST['news_disablecomments'] == "on")
	{
		$disable_comments = 1;
	}
	
		$time_then = mktime(date('H', $time_now), date('i', $time_now), date('s', $time_now), $news['month'], $news['day'], $news['year']);
		$sql = "INSERT INTO " . NEWS_TABLE . " SET ".
		"news_user_id='" . $USER->getId() . "', ".
		"news_title='" . $news['title'] . "', ".
		"news_date='" . $time_then . "', ".
		"news_message='" .  $news['comment'] . "', ".
		"news_category='" .  $news['category'] . "', ".
		"news_disable_comment='" . $disable_comments . "'";
		if(!$result = $db->sql_query($sql))
		{
			printError("SQL ERROR", mysql_error($sql));
		}	
			
			$id = mysql_insert_id();
			
			echo"<br /><br /><br /><br /><br /><br /><br /><br /><br />
			<center><meta http-equiv=\"refresh\"content=\"3;url=index.php?action=news&sub=show&nid=$id\">";
			echo "<h1>" . ucfirst(translate('news_submited')) . "</h1>";
			echo "<br/>" . translate('redirectedin') . " <a href=\"index.php?action=news&sub=show&nid=$id\">" . translate('clickhere') . "</a></center>
			<div class='fixed'>
					</div>
				</div>
				</div>
			<div class='fixed'>
			</div>
			<div id='footer'>
			<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a>
				<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br />
                </div>";
			exit;	
}
$array_cat = getNewsCat();
?>

<!-- TinyMCE -->
<script type="text/javascript" src="jscripts/tiny_mce/tiny_mce.js"></script>

<script type="text/javascript">
tinyMCE.init({
        // General options
		language : "<?php echo $USER->getLang(); ?>",
         mode : "textareas",
        theme : "advanced",
        plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager",

        // Theme options
        theme_advanced_buttons1 : "save,newdocument,|,undo,redo,|,cut,copy,paste,pastetext,pasteword,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect|,forecolor,backcolor,|,charmap,emotions,iespell,media,advhr,|,image,insertimage",
		
        theme_advanced_buttons2 : "search,replace,bullist,numlist,|,outdent,indent,blockquote,|,link,unlink,anchor,cleanup,code,|,insertdate,inserttime,preview,|,hr,removeformat,visualaid,|,sub,sup,|,print,|,fullscreen,|,insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak",
		
		theme_advanced_buttons3 : "",
		
        theme_advanced_buttons4 : "",
		
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
        theme_advanced_resizing : true,

        // Skin options
        skin : "o2k7",
        skin_variant : "silver",
});
</script>

<!-- /TinyMCE -->

<div id='postpath'>
	<a title="<?php echo translate('gotofrontpage'); ?>" href="index.php"><?php echo translate('home'); ?></a>
	&gt; <a href="index.php?action=profile&sub=edit" title='<?php echo translate('edit'); ?>'><?php echo translate('edit'); ?></a> 
	&gt; <a href="index.php?action=profile&sub=edit_admin" title='<?php echo translate('aminsettings'); ?>'><?php echo translate('adminsettings'); ?></a> 
    &gt; <a href="index.php?action=news&sub=new" title='<?php echo translate('new_article'); ?>'><?php echo translate('new_article'); ?></a> 
</div>
<br />
<h1><?php echo translate('writeanarticle'); ?></h1>
    
<form name="news_form" method="post" action="index.php?action=news&sub=new">
	<br />
	<b><?php echo translate('title'); ?>:</b> <input type="text" name="news_title" size="50" value="<?php echo $news['title']; ?>"/> 
	<?php 
	if(isset($missing['title'])) 
	{
		echo "<span class=\"error\"><-- " . translate($missing['title']) . "</span>";
	}
	?>
    <b><?php // echo translate('date'); ?></b>
    <select class="select" id="day" name="day">
        <option value=""><?php echo ucfirst(translate('day')); ?></option>
        <?php
        for($i=1; $i<=31; $i++)
        {
            if($i == $news['day'])
            {
                echo "<option value=\"" . $i . "\" selected=\"selected\">" . $i . "</option>\n";
            }
            else
            {
                echo "<option value=\"" . $i . "\">" . $i . "</option>\n";
            }
        }
        ?>
    </select>
    <select class="select" id="month" name="month">
        <option value=""><?php echo ucfirst(translate('month')); ?></option>
        <?php
        foreach($MONTHS as $k => $v)
        {
            if(($k+1) == $news['month'])
            {
                echo "<option value=\"" . ($k+1) . "\" selected=\"selected\">" . ucfirst(translate($v)) . "</option>\n";
            }
            else
            {
                echo "<option value=\"" . ($k+1) . "\">" . ucfirst(translate($v)) . "</option>\n";
            }
        }
        ?>
    </select>
    <select class="select" id="year" name="year">
        <option value=""><?php echo ucfirst(translate('year')); ?></option>
        <?php
        for($i=date('Y', $time_now); $i>=(date('Y', $time_now) - 2); $i--)
        {
            if($i == $news['year'])
            {
                echo "<option value=\"" . $i . "\" selected=\"selected\">" . $i . "</option>\n";
            }
            else
            {
                echo "<option value=\"" . $i . "\">" . $i . "</option>\n";
            }
        }
        ?>
    </select> <img src="img/upd.gif" title="<?php echo translate('date'); ?>"/>
    <?php 
	if(isset($missing['day']) or isset($missing['month']) or isset($missing['year']))
	{
		echo "<span class=\"error\"><-- " . translate('missing') . "</span>";
	}
	?>
	<br />
    <br />
    
    <b><?php echo ucf(translate('category')); ?>:</b>
        <select id="news_category" name="news_category">
				<?php
					foreach($array_cat as $val)
					{
							if($news['category'] == $val['cat_name'] || $news['category'] != "")
							{
								echo "<option value=\"" . $val['cat_name'] . "\" selected=\"selected\">" . translate($val['cat_name']) . "</option>\n";
							}
							else
							{
								echo "<option value=\"" . $val['cat_name'] . "\">" . translate($val['cat_name']) . "</option>\n";
								
							}
					}
					echo "<option value=\"uncategorized\">" . translate('nocat') . "</option>\n";
				
				?>
				</select>
                <a href="index.php?action=news&sub=new_cat"><img src="img/add.png" /><?php echo translate('addnewcat'); ?></a> 
    <?php 
	if(isset($missing['category'])) 
	{
		echo "<span class=\"error\"><-- " . translate($missing['category']) . "</span>";
	}
	?>   
    
    <br />
    <br /> 
        
	<b><?php echo translate('comment'); ?>:</b>
	<?php 
	if(isset($missing['comment'])) 
	{

		echo "<span class=\"error\"><-- " . translate($missing['comment']) . "</span>";
	}
	?>
	<br />
	<textarea id="comment" name="news_message" rows="10" cols="90"><?php echo $news['comment']; ?></textarea><br />
	<center>
		
		<br />
		<img src="img/com.png" /> <b><?php echo translate('disable_commenting'); ?></b>
		<?php
		echo "<input type=\"checkbox\" name=\"news_disablecomments\" ";
		if(isset($disable_comments) && $disable_comments == 1)
		{
			echo "checked=\"checked\"";
		}
		echo " />";
		?><br />
		<div class="set_subtext"><?php echo translate('enableordisablecommentsonyourblog'); ?></div>
		<br />
		<input id="submit" style="font-weight:bold;" name="news_submit" type="submit" value=" <?php echo translate('submitnews'); ?> " /> 
		<input type="reset" style="font-weight:bold;" value=" <?php echo translate('clearallinputs'); ?> " />
	</center>
</form>
    
<?php
}
else
{
	header('Location: index.php');
	exit;
}
?>




